package com.fametal.ui.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ActivityGroup;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;

import com.fametal.GlobalConstant.Settings;
import com.fametal.R;
import com.fametal.ui.utils.Log;
import com.fametal.ui.utils.Utils;
import com.fametal.ui.utils.Utils.GetFinishedListener;
import com.fametal.ui.widget.CustomSeekBar;
import com.fametal.ui.widget.CustomSeekBar.OnClickListener;

public class AudioControlActivity extends Activity implements GetFinishedListener, OnClickListener{

	private final static String AUDIO_CONTROL_URL = Settings.IP + "cgi-bin/audio_control.cgi?channel=all&operation=getinitvalue&volume=NULL";
	private static final int MAX = 0;
	private static final int MIN = -63;
	private ActivityGroup mGroup;
	private String inputValue;
	private String outputValue;
	private View lastInputView;
	private View lastOutputView;
	
	private int videoMeeting;
	private int outConnectPc;
	private int dvd;
	private int obligateOne;
	private int outConnectMicrophone;
	private int phoneMeeting;
	private int obligateTwo;
	private int localAmplification;
	private int beyondMeeting;
	private int beyondPhone;
	
	private CustomSeekBar videoMeetingSeekBar;
	private CustomSeekBar outConnectPcSeekBar;
	private CustomSeekBar dvdSeekBar;
	private CustomSeekBar obligateOneSeekBar;
	private CustomSeekBar outConnectMicrophoneSeekBar;
	private CustomSeekBar phoneMeetingSeekBar;
	private CustomSeekBar obligateTwoSeekBar;
	private CustomSeekBar localAmplificationSeekBar;
	private CustomSeekBar beyondMeetingSeekBar;
	private CustomSeekBar beyondPhoneSeekBar;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.audio_control);
		mGroup = (ActivityGroup)getParent();
		
		videoMeetingSeekBar = (CustomSeekBar)findViewById(R.id.video_meeting_volume);
		outConnectPcSeekBar = (CustomSeekBar)findViewById(R.id.out_connect_pc_volume);
		dvdSeekBar = (CustomSeekBar)findViewById(R.id.dvd_volume);
		obligateOneSeekBar = (CustomSeekBar)findViewById(R.id.obligate_one_volume);
		outConnectMicrophoneSeekBar = (CustomSeekBar)findViewById(R.id.out_connect_microphone_volume);
		phoneMeetingSeekBar = (CustomSeekBar)findViewById(R.id.phone_meeting_volume);
		obligateTwoSeekBar = (CustomSeekBar)findViewById(R.id.obligate_two_volume);
		localAmplificationSeekBar = (CustomSeekBar) findViewById(R.id.local_amplification_volume);
		beyondMeetingSeekBar = (CustomSeekBar) findViewById(R.id.beyond_meeting_volume);
		beyondPhoneSeekBar = (CustomSeekBar) findViewById(R.id.beyond_phone_volume);
		
		videoMeetingSeekBar.setScope(MAX, MIN);
		videoMeetingSeekBar.setOnClickListener(this);
		outConnectPcSeekBar.setScope(MAX, MIN);
		outConnectPcSeekBar.setOnClickListener(this);
		dvdSeekBar.setScope(MAX, MIN);
		dvdSeekBar.setOnClickListener(this);
		obligateOneSeekBar.setScope(MAX, MIN);
		obligateOneSeekBar.setOnClickListener(this);
		outConnectMicrophoneSeekBar.setScope(MAX, MIN);
		outConnectMicrophoneSeekBar.setOnClickListener(this);
		phoneMeetingSeekBar.setScope(MAX, MIN);
		phoneMeetingSeekBar.setOnClickListener(this);
		obligateTwoSeekBar.setScope(MAX, MIN);
		obligateTwoSeekBar.setOnClickListener(this);
		localAmplificationSeekBar.setScope(MAX, MIN);
		localAmplificationSeekBar.setOnClickListener(this);
		beyondMeetingSeekBar.setScope(MAX, MIN);
		beyondMeetingSeekBar.setOnClickListener(this);
		beyondPhoneSeekBar.setScope(MAX, MIN);
		beyondPhoneSeekBar.setOnClickListener(this);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		Utils.setGetFinishedListener(this);
		Utils.get(mGroup, AUDIO_CONTROL_URL, true);
	}
	
	@Override
	public void finished(String result) {
		try {
			JSONObject jsonObject = new JSONObject(result);
			JSONObject singletrack = jsonObject.getJSONObject("singletrack");
			videoMeeting = singletrack.getInt("vol1");
			outConnectPc = singletrack.getInt("vol2");
			dvd = singletrack.getInt("vol3");
			obligateOne = singletrack.getInt("vol4");
			outConnectMicrophone = singletrack.getInt("vol5");
			phoneMeeting = singletrack.getInt("vol6");
			obligateTwo = singletrack.getInt("vol7");
			JSONObject mainoutput = jsonObject.getJSONObject("mainoutput");
			localAmplification = mainoutput.getInt("vol1");
			JSONObject auxiliaryoutput = jsonObject.getJSONObject("auxiliaryoutput");
			beyondMeeting = auxiliaryoutput.getInt("vol1");
			beyondPhone = auxiliaryoutput.getInt("vol2");
			Log.d("david", videoMeeting + "::" + outConnectPc + "::" + dvd + "::" + obligateOne
					+ "::" + outConnectMicrophone + "::" + phoneMeeting + "::" + obligateTwo 
					+ "::" + localAmplification + "::" + beyondMeeting + "::" + beyondPhone);
			initVolume();
		} catch (JSONException e) {
			e.printStackTrace();
		} 
	}
	
	private void initVolume(){
		videoMeetingSeekBar.setProgress(videoMeeting);
		outConnectPcSeekBar.setProgress(outConnectPc);
		dvdSeekBar.setProgress(dvd);
		obligateOneSeekBar.setProgress(obligateOne);
		outConnectMicrophoneSeekBar.setProgress(outConnectMicrophone);
		phoneMeetingSeekBar.setProgress(phoneMeeting);
		obligateTwoSeekBar.setProgress(obligateTwo);
		
		localAmplificationSeekBar.setProgress(localAmplification);
		beyondMeetingSeekBar.setProgress(beyondMeeting);
		beyondPhoneSeekBar.setProgress(beyondPhone);
	}
	
	@Override
	public boolean onClick(View view, int progress, int mode) {
		String name = ""; 
		String op = "";
		Log.d("david", "id = " + view.getId());
		switch (view.getId()) {
		case R.id.video_meeting_volume:
			name = "sdaudio1";
			break;
		case R.id.out_connect_pc_volume:
			name = "sdaudio2";
			break;
		case R.id.dvd_volume:
			name = "sdaudio3";
			break;
		case R.id.obligate_one_volume:
			name = "sdaudio4";
			break;
		case R.id.out_connect_microphone_volume:
			name = "sdaudio5";
			break;
		case R.id.phone_meeting_volume:
			name = "sdaudio6";
			break;
		case R.id.obligate_two_volume:
			name = "sdaudio7";
			break;
		case R.id.local_amplification_volume:
			name= "sfaudio1";
			break;
		case R.id.beyond_meeting_volume:
			name= "sfaudio2";
			break;
		case R.id.beyond_phone_volume:
			name= "sfaudio3";
			break;
		}
		if(mode == CustomSeekBar.INCREASE){
			op = "+";
		}else if(mode == CustomSeekBar.DECREASE){
			op = "-";
		}
//		Log.d("david", "channel = " + name + "; operation = " + op + "progress = " + progress);
		String url = Settings.IP + "cgi-bin/audio_control.cgi?channel=" + name + "&operation=" + op + "&volume=" + progress;
		Utils.get(mGroup, url, false);
		return true;
	}
	
	public void onClick(View view) {
		String operation = null;
		view.requestFocusFromTouch();
		switch (view.getId()) {
		case R.id.balanced_control:
			LinearLayout container = (LinearLayout) mGroup.getWindow().findViewById(R.id.audio_control_container);
			mGroup.getLocalActivityManager().removeAllActivities();
			Utils.setActivityContent(mGroup, "audio", container, BalancedControlActivity.class);
			return;
		case R.id.video_meeting:   //视频会议
			selectedInput(view);
			inputValue = "01";
			return;
		case R.id.out_connect_pc:  //外接pc
			selectedInput(view);
			inputValue = "02";
			return;
		case R.id.dvd:   //dvd
			selectedInput(view);
			inputValue = "03";
			return;
		case R.id.obligate_one:  //预留
			selectedInput(view);
			inputValue = "04";
			return;
		case R.id.out_connect_microphone:   //外接话筒
			selectedInput(view);
			inputValue = "05";
			return;
		case R.id.phone_metting:       //电话会议
			selectedInput(view);
			inputValue = "06";
			return;
		case R.id.obligate_two:         //预留
			selectedInput(view);
			inputValue = "07";
			return;
		case R.id.local_amplification:     //本地扩音
			selectedOutput(view);
			outputValue = "16";
			get();
			return;
		case R.id.beyond_meeting:       //异地会场
			selectedOutput(view);
			outputValue = "9";
			get();
			return;
		case R.id.beyond_phone:      //异地电话
			selectedOutput(view);
			outputValue = "10";
			get();
			return;	
		case R.id.regular_meeting:
			operation = "normal";
			break;
		case R.id.video_meeting_bt:
			operation = "video";
			break;
		case R.id.phone_meeting_bt:
			operation = "telecom";
			break;
		case R.id.onekey_mute:
			operation = "muteall";
			break;
		}
		if(!TextUtils.isEmpty(operation)) {
			String url = Settings.IP + "cgi-bin/audio_control?operation" + operation;
			Utils.get(mGroup, url, false);
		}
	}
	
	public void get() {
		String url = Settings.IP + "cgi-bin/audio_control.cgi?channel="+inputValue+"&"+"operation=to"+"&"+"volume="+outputValue;
		Utils.get(mGroup, url, false);
	}
	
	public void selectedInput(View view) {
		view.setBackgroundResource(R.drawable.channel_select_down);
		if (lastInputView != null) lastInputView.setBackgroundResource(R.drawable.channel_select_up);
		lastInputView = view;
	}
	
	public void selectedOutput(View view) {
		view.setBackgroundResource(R.drawable.channel_select_down);
		if (lastOutputView != null) lastOutputView.setBackgroundResource(R.drawable.channel_select_up);
		lastOutputView = view;
	}
}
